#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :文件操作.py
# @Time      :2024/3/12 
# @Author    :CL
# @email     :1037654919@qq.com
import os

from utils import mongo_manager

sucai_title_code = mongo_manager("sucai_title_code", db='car_images')
def count_files_in_folder(folder_path):
    file_count = len([name for name in os.listdir(folder_path) if os.path.isfile(os.path.join(folder_path, name))])
    return file_count

def delete_empty_folders(root_folder):
    for root, dirs, files in os.walk(root_folder, topdown=False):
        for dir_name in dirs:
            dir_path = os.path.join(root, dir_name)
            if not os.listdir(dir_path):  # 如果文件夹为空
                print(f"Deleting empty folder: {dir_path}")
                os.rmdir(dir_path)

path = '/media/chenglei3/77D014CED257D1411/images/hippopx'
folders = [f for f in os.listdir(path) if os.path.isdir(os.path.join(path, f))]

for folder_name in folders:
    folder_path = os.path.join(path, folder_name)
    file_count = count_files_in_folder(folder_path)
    print(f"Folder '{folder_name}' contains {file_count} files.")
    if file_count > 100: # 如果已经下载超过100 ，不再下载
        print(f"Folder '{folder_name}' is already downloaded.")
        sucai_title_code.updateOne({"title_en": folder_name}, {"file_count": file_count,"hippopx": 'done'})
    else:
        sucai_title_code.updateOne({"title_en": folder_name}, {"file_count": file_count})

delete_empty_folders(path)
#
if __name__ == "__main__":
    print()